package com.aliyun.util;

import com.aliyun.listerner.MyMessageOrderListener;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.order.OrderConsumer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class ConsumerUtil {
	Properties properties = new Properties();
	public ConsumerUtil(){
		properties.put(PropertyKeyConst.ConsumerId, "CID_cs2");// 
        properties.put(PropertyKeyConst.AccessKey, "LTAIPbDaVfZSlkqo");// 
        properties.put(PropertyKeyConst.SecretKey, "1nwgUpL1dbyyRd5wgDxDv0i6dc5k4g");// 
        properties.put(PropertyKeyConst.ONSAddr,
                "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");//
        properties.put(PropertyKeyConst.SuspendTimeMillis, "100"); // 
        properties.put(PropertyKeyConst.MaxReconsumeTimes, "20"); // 
	}
    public void getMessage(String text) {
    	
    	
    	File file = new File("c://zhifubao.txt");
		if (file.isFile() && file.exists()) {
			try {
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), "UTF-8");
				BufferedReader reader = new BufferedReader(read);
				String line;
				try {
					while ((line = reader.readLine()) != null) {
						String[] zhanghao_mima = line.split(",");
				        MyMessageOrderListener mo_lis = new MyMessageOrderListener(zhanghao_mima[0],zhanghao_mima[2]);
				        OrderConsumer consumer = ONSFactory.createOrderedConsumer(properties);
				        consumer.subscribe(
				                "top_cs",
				                text,
				                mo_lis);
				        consumer.start();
					}
					reader.close();
					read.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
    }
}
